﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Warehouse.UI.WinForm.Forms
{
    public partial class EditForm : Form
    {
        #region -----属性--------

        public string TitleText
        {
            get { return _titleText; }
            set
            {
                _titleText = value;
                this.Text = value;
            }
        }
        private string _titleText;

        protected bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                if (_isBusy) this.Cursor = Cursors.WaitCursor;
                //listPart.BusyState = value;
                if (!_isBusy) this.Cursor = Cursors.Default;
            }
        }
        private bool _isBusy;

        public EditState EditState
        {
            get { return _editState; }
            set
            {
                _editState = value;
                //listPart.ListState = value;
            }
        }
        private EditState _editState;

        /// <summary>
        /// 记住返回值
        /// </summary>
        protected DialogResult RememberDialogResult { get; set; }

        protected Timer timer1;
        protected BindingSource bs;
        protected ErrorProvider ep;

        #endregion -----------

        //--未实现----------
        protected virtual void InitLoad() { }
        protected virtual void InitShown() { }

        protected virtual bool GetByID(int id) { throw new NotImplementedException("需要重载"); }
        protected virtual bool StartAdd() { throw new NotImplementedException("需要重载"); }
        protected virtual bool StartDelete(object toDelete) { throw new NotImplementedException("需要重载"); }
        protected virtual bool Save() { throw new NotImplementedException("需要重载"); }
        protected virtual bool Undo() { throw new NotImplementedException("需要重载"); }
        protected virtual bool CheckValid() { throw new NotImplementedException("需要重载"); }
        //--已实现----------
        protected virtual bool Export()
        {
            //MessageBox.Show(this.EntryId.ToString()); 
            return true;
        }

        protected virtual void DataChanged(object sender, EventArgs e)
        {
            this.EditState = this.EditState & (~EditState.Unchanged);
            this.EditState = this.EditState | EditState.Modified;
            var ctl = (this.ActiveControl);
            if (ep.GetError(ctl) != string.Empty) ep.SetError(ctl, string.Empty);
        }

        public virtual bool Add()
        {
            if (StartAdd())
            {
                this.EditState = EditState.Added;
                return true;
            }
            return false;
        }

        private void StartEdit()
        {
            this.EditState = EditState.Unchanged;
            Utility.FocusContainer(groupBoxContent);
        }

        public virtual bool Edit(int id)
        {
            if (GetByID(id))
            {
                StartEdit();
                return true;
            }
            return false;
        }

        public virtual bool Delete(int id)
        {
            if (GetByID(id))
            {
                this.EditState = EditState.Deleted;
                return true;
            }
            return false;
        }

        public virtual bool View(int id)
        {
            if (GetByID(id))
            {
                this.EditState = EditState.ReadOnly;
                return true;
            }
            return false;
        }

        //--Constructor
        public EditForm()
        {
            InitializeComponent();

            this.RememberDialogResult = DialogResult.Cancel;
            ep = new ErrorProvider { BlinkStyle = ErrorBlinkStyle.NeverBlink };
            bs = new BindingSource();
        }

        private void EditForm_Load(object sender, EventArgs e)
        {
            InitLoad();
        }

        private void EditForm_Shown(object sender, EventArgs e)
        {
            InitShown();
            if (this.EditState == EditState.Deleted)
                btnDele_Click(sender, e);
        }

        private void EditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = _checkChanges() == false;
            this.DialogResult = this.RememberDialogResult;
        }

        private void EditForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(13))
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");
            }
        }

        private bool _checkChanges()
        {
            this.bs.EndEdit();
            if ((this.EditState & EditState.Modified) != 0)
            {
                switch (Utility.ShowChoose2("数据已经改变，是否保存？", this, "保存"))
                {
                    case DialogResult.Yes:
                        if (CheckValid() && Save())
                        {
                            this.RememberDialogResult = DialogResult.OK;
                            return true;
                        }
                        return false;
                    case DialogResult.No:
                        return true;
                    default:
                        return false;
                }
            }
            return true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.IsBusy = true;
            try
            {
                Add();
            }
            finally
            {
                this.IsBusy = false;
            }
        }

        private void btnDele_Click(object sender, EventArgs e)
        {
            //
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //
        }

        private void btnUndo_Click(object sender, EventArgs e)
        {
            //
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            //
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
